//入口公用方法
define([''],function(require,exports, module){

  var baseUrl = window.location.protocol+'//'+window.location.host;
  function main(){
  	this.baseUrl =baseUrl;
  }
  module.exports = main;  
  // 左边菜单加载方法
  main.prototype.loadmenu = function(obj,index){
      $(obj).siblings('li').removeClass('active');
      $(obj).addClass('active');
      $(obj+' ul li').eq(index).addClass('active');
  }
  main.prototype.ajax = function(url,data,callback){
      var opt = '',num = arguments.length;
      if(num == 2){
        opt = arguments[1];
        arguments[2] = opt;
        arguments[1] = '';
      }
      $.ajax({
        url:arguments[0],
        data:{data:arguments[1]},
        type:'post',
        dataType:'jsonp',
        jsonp:'callback',
        success:arguments[2]
      });
   }
   // 巧姐 new
  main.prototype.newRemind = function(des,cb){
       html = '<div class="promote_style">'
                    html+='<div class="cont">'+des+'</div>\
                    <div class="btn"><p>取消</p><div id="promptBoxSure">确定</div></div>\
                </div>\
                <div id="mask" style="display:block"></div>';
       $('#promptBoxNew').html(html);
       var boxH = $('.promote_style').height();
       $('.promote_style').css({'margin-top':-boxH/1.5});

       // 取消
       $('#promptBoxNew .promote_style .btn p').click(function(){
          $('#promptBoxNew').html('');
       })
       // 确定
       $('#promptBoxNew #promptBoxSure').click(function(){
          typeof cb == "function" && cb()
       })
  }
  //空白页面
  main.prototype.hist=function(content,ico,id,size){
      var histhtml='<div class="p_Blankpages">\
              <div class="p_Blankpages2">\
                <div class="p_blank_ico">\
                   <span class="'+ico+'" style="font-size:'+size+'"></span>\
                </div>\
                <div class="p_blank_hist">'+content+'</div>\
                </div>\
            </div>';
       $(id).html(histhtml);
  }
  //手机号码格式判断
  main.prototype.IsPhone = function(phone) {
     var re;
     re= /^1[0-9]{10}$/;
     if(re.test(phone)){
          return true;
       }else{
          return  false;
     };
  }
    //固定电话判断
    main.prototype.IsTel = function(phone) {
        var re;
        re= /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/
        if(re.test(phone)){
            return true;
        }else{
            return  false;
        };
    }
  main.prototype.Email = function(val){
    var ret = val.match(/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/)
    if(!ret){
      return false;
    }else{
      return true;
    }
  }
  //银行卡号码格式判断
  main.prototype.IsBankCard = function(card){
    if(card.length>=16 && card.length<=19){
      return true;
    }else{
      return false;
    }
  }
  //银行卡号校验
//Description: 银行卡号Luhm校验
//Luhm校验规则：16位银行卡号（19位通用）:
// 1.将未带校验位的 15（或18）位卡号从右依次编号 1 到 15（18），位于奇数位号上的数字乘以 2。
// 2.将奇位乘积的个十位全部相加，再加上所有偶数位上的数字。
// 3.将加法和加上校验位能被 10 整除。
    main.prototype.luhmCheck = function(bankno){
        if (bankno.length < 16 || bankno.length > 19) {
            //$("#banknoInfo").html("银行卡号长度必须在16到19之间");
            return false;
        }
        var num = /^\d*$/; //全数字
        if (!num.exec(bankno)) {
            //$("#banknoInfo").html("银行卡号必须全为数字");
            return false;
        }
        //开头6位
        var strBin="10,18,30,35,37,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,58,60,62,65,68,69,84,87,88,94,95,98,99";
        if (strBin.indexOf(bankno.substring(0, 2))== -1) {
            //$("#banknoInfo").html("银行卡号开头6位不符合规范");
            return false;
        }
        var lastNum=bankno.substr(bankno.length-1,1);//取出最后一位（与luhm进行比较）
        var first15Num=bankno.substr(0,bankno.length-1);//前15或18位
        var newArr=new Array();
        for(var i=first15Num.length-1;i>-1;i--){ //前15或18位倒序存进数组
            newArr.push(first15Num.substr(i,1));
        }
        var arrJiShu=new Array(); //奇数位*2的积 <9
        var arrJiShu2=new Array(); //奇数位*2的积 >9
        var arrOuShu=new Array(); //偶数位数组
        for(var j=0;j<newArr.length;j++){
            if((j+1)%2==1){//奇数位
                if(parseInt(newArr[j])*2<9)
                    arrJiShu.push(parseInt(newArr[j])*2);
                else
                    arrJiShu2.push(parseInt(newArr[j])*2);
            }
            else //偶数位
                arrOuShu.push(newArr[j]);
        }
        var jishu_child1=new Array();//奇数位*2 >9 的分割之后的数组个位数
        var jishu_child2=new Array();//奇数位*2 >9 的分割之后的数组十位数
        for(var h=0;h<arrJiShu2.length;h++){
            jishu_child1.push(parseInt(arrJiShu2[h])%10);
            jishu_child2.push(parseInt(arrJiShu2[h])/10);
        }
        var sumJiShu=0; //奇数位*2 < 9 的数组之和
        var sumOuShu=0; //偶数位数组之和
        var sumJiShuChild1=0; //奇数位*2 >9 的分割之后的数组个位数之和
        var sumJiShuChild2=0; //奇数位*2 >9 的分割之后的数组十位数之和
        var sumTotal=0;
        for(var m=0;m<arrJiShu.length;m++){
            sumJiShu=sumJiShu+parseInt(arrJiShu[m]);
        }
        for(var n=0;n<arrOuShu.length;n++){
            sumOuShu=sumOuShu+parseInt(arrOuShu[n]);
        }
        for(var p=0;p<jishu_child1.length;p++){
            sumJiShuChild1=sumJiShuChild1+parseInt(jishu_child1[p]);
            sumJiShuChild2=sumJiShuChild2+parseInt(jishu_child2[p]);
        }
        //计算总和
        sumTotal=parseInt(sumJiShu)+parseInt(sumOuShu)+parseInt(sumJiShuChild1)+parseInt(sumJiShuChild2);
        //计算Luhm值
        var k= parseInt(sumTotal)%10==0?10:parseInt(sumTotal)%10;
        var luhm= 10-k;
        if(lastNum==luhm){
            //$("#banknoInfo").html("Luhm验证通过");
            return true;
        }
        else{
            //$("#banknoInfo").html("银行卡号必须符合Luhm校验");
            return false;
        }
    }

    //身份证号格式判断
    main.prototype.isCardNo=function(sId){
        var aCity = {11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"}
        //var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        var iSum=0 ;
        var info="" ;
        if(!/^\d{17}(\d|x)$/i.test(sId)) return "请输入正确的身份证号";
        sId=sId.replace(/x$/i,"a");
        if(aCity[parseInt(sId.substr(0,2))]==null) return "你的身份证地区非法";
        sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));
        var d=new Date(sBirthday.replace(/-/g,"/")) ;
        if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))return "身份证上的出生日期非法";
        for(var i = 17;i>=0;i --) iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11) ;
        if(iSum%11!=1) return "你输入的身份证号非法";
        //aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"女");//此次还可以判断出输入的身份证号的人性别
        return true;
    }


  //获取cookie是否存在
  main.prototype.getCookie=function(name){
  	 var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
  	  	if(arr != null) return unescape(arr[2]);
  			return null;
  }
  //截取时间戳
  main.prototype.substrTime = function(Dtime){
    return parseInt(Dtime.substr(6, 13));
  }

  //时间戳格式化
  main.prototype.format = function(timestamp) {
      var data = new Date(timestamp);
      var year=data.getFullYear(); 
      var month=data.getMonth()+1;
      month=month<10?'0'+month:month;
      var date=data.getDate();
      date=date<10?'0'+date:date;
      return year+"-"+month+"-"+date; 
  }
  //时间戳格式化(年-月-日)
  main.prototype.formatYmd1 = function(timestamp){
      var timestamp = parseInt(timestamp.substr(6, 13));
      var data = new Date(timestamp);
      var year=data.getFullYear();
      var month=data.getMonth()+1;
      var month = month < 10 ? '0'+ month :month;
      var day=data.getDate();
      day = day < 10 ? '0'+ day :day;
      return year+'-'+month+"-"+day;
  }
  //时间戳格式化(年/月)
  main.prototype.yearmonth = function(timestamp){
    var data = new Date(timestamp);
    var year=data.getFullYear(); 
    var month=data.getMonth()+1; 
    return year+"/"+month;
  }


  //时间戳格式化(月/日)
  main.prototype.monthday = function(timestamp){
    var timestamp = parseInt(timestamp.substr(6, 13));
    var data = new Date(timestamp);
    var month=data.getMonth()+1; 
    var day=data.getDate(); 
    return month+"月"+day+"日";
  }

  //时间戳格式化(年-月-日 时:分:秒)
  main.prototype.formatTimeAll = function(timestamp){
    var timestamp = parseInt(timestamp.substr(6, 13));
    var data = new Date(timestamp);
    var year=data.getFullYear();
    var month=data.getMonth()+1;
    var day=data.getDate();
    var h=data.getHours();
    h=h<10?('0'+h):h;
    var i=data.getMinutes();
    i=i<10?('0'+i):i;
    var s=data.getSeconds();
    s=s<10?('0'+s):s;
    return year+'-'+month+"-"+day+" "+h+':'+i+':'+s;
  }
  //时间戳格式化(年-月-日 时:分)
  main.prototype.formatTimeAll2 = function(timestamp){
    var timestamp = parseInt(timestamp.substr(6, 13));
    var data = new Date(timestamp);
    var year=data.getFullYear();
    var month=data.getMonth()+1;
      month=month<10?('0'+month):month;
    var day=data.getDate();
    var h=data.getHours();
    h=h<10?('0'+h):h;
    var i=data.getMinutes();
    i=i<10?('0'+i):i;
    var s=data.getSeconds();
    s=s<10?('0'+s):s;
    return year+'-'+month+"-"+day+" "+h+':'+i;
  }

    //时间戳格式化(年-月-日 时:分)
    main.prototype.formatTimeAll3 = function(timestamp){
        var timestamp = parseInt(timestamp.substr(6, 13));
        var data = new Date(timestamp);
        var month=data.getMonth()+1;
        month = month < 10 ? ( '0' + month ) : month;
        var day = data.getDate();
        var h = data.getHours();
        h = h < 10 ? ( '0' + h ) : h;
        var i = data.getMinutes();
        i = i < 10 ? ( '0' + i ) : i;
        var s = data.getSeconds();
        s = s < 10 ? ( '0' + s ) : s;
        return month + "-" + day + "  " + h + ':' + i;
    }








  //时间戳格式化(年月日 时:分)
  main.prototype.formatLange = function(timestamp){
    var timestamp = parseInt(timestamp.substr(6, 13));
    var data = new Date(timestamp);
    var year=data.getFullYear();
    var month=data.getMonth()+1;
    var day=data.getDate();
    var h=data.getHours();
    h = h < 10 ? ( '0' + h ) : h;
    var i = data.getMinutes();
    i=i<10?('0'+i):i;
    var s=data.getSeconds();
    s=s<10?('0'+s):s;
    return year+'年'+month+"月"+day+"日 "+h+':'+i;
  }


    //时间戳格式化(年月日 时:分)
    main.prototype.formatLange2 = function(data){
        var year = data.getFullYear();
        var month = data.getMonth()+1;
        month = month < 10 ? month :'0'+month;
        var day = data.getDate();
        var h = data.getHours();
        h = h<10?('0'+h):h;
        var i= data.getMinutes();
        i=i<10?('0'+i):i;
        var s=data.getSeconds();
        s=s<10?('0'+s):s;
        return year+'年'+month+"月"+day+"日 "+h+':'+i;
    }


  //时间戳格式化(年-月-日)
  main.prototype.formatYmd = function(timestamp){
    var timestamp = parseInt(timestamp.substr(6, 13));
    var data = new Date(timestamp);
    var year = data.getFullYear();
    var month = data.getMonth()+1;
    var day = data.getDate();
      month =   month < 10 ? ('0'+month) : month;
      day = day < 10 ? ('0'+day) : day;
    return year+'-'+month+"-"+day;
  }
  main.prototype.formatYmdtext = function(timestamp){
    var timestamp = parseInt(timestamp.substr(6, 13));
    var data = new Date(timestamp);
    var year=data.getFullYear();
    var month=data.getMonth()+1;
    var day =data.getDate();
      month =   month < 10 ? ('0'+month) : month;
      day = day < 10 ? ('0'+day) : day;
    return year+'年'+month+"月"+day+'日';
  }
    main.prototype.formatYmdtext1 = function(data){
    var year=data.getFullYear();
    var month=data.getMonth()+1;
    var day =data.getDate();
      month =   month < 10 ? ('0'+month) : month;
      day = day < 10 ? ('0'+day) : day;
    return year+'年'+month+"月"+day+'日';
  }






  //时间戳格式化(时:分:秒)
  main.prototype.formatHis = function(timestamp){
    var timestamp = parseInt(timestamp.substr(6, 13));
    var data = new Date(timestamp);
    var h=data.getHours();
    h=h<10?('0'+h):h;
    var i=data.getMinutes();
    i=i<10?('0'+i):i;
    var s=data.getSeconds();
    s=s<10?('0'+s):s;
    return h+':'+i+':'+s;
  }
  //分割.net 返回时间多个T
  main.prototype.Timeset=function(text,status){
       var a;
       var b;
       switch(status){
          case 1:
            a =text.split("T");
            b=a[0];
          break; 
          case 2:
            a =text.split("T");
            b=a[1];
          break;
          default:
            a =text.replace("T","\n");
            b=a;
          break;
       }
       return b;
  }
  // 登录注册 完成 弹框
  /*
    @ icon 需要显示icon 就传 不需要就不传
    @ des 提示信息
    @ time 消失时间
    */ 
  main.prototype.MaskInfo = function(des,icon,fadeout){
    var  fadeouts = fadeout ? fadeout : 1500;
    var  str = '<div class="login-modal dialog">\
                  <div class="login-modal-wraper">\
                      <div class="login-modal-wrap animatedfade bounceIn">\
                          <div class="login-modal-cont">\
                              <p>\
                                  <span class="icon '+ icon +'" style="color: #fff;"></span>\
                                  <span class="login-modal-alert">'+ des +'</span>\
                              </p>\
                          </div>\
                      </div>\
                  </div>\
              </div>'
    $('#MaskInfo').html(str);
    setTimeout(function(){
      $('#MaskInfo').html('');
    },fadeouts)
  }
 
   //储存URL信息到localStorage
  main.prototype.UrltoStorage  = function () {
      var url = window.document.location.href.toString();
      var u = url.split("?");
      var url_main = u[0].split("/");
      var method = url_main.pop();
      var controller = url_main.pop();
      if((controller == 'site' && method == 'index')||(controller == 'site' && method == 'find')||(controller == 'personal' && method == 'index')){
        //如果当前页面为首页则清除localstorage.historyURL
        localStorage.removeItem('historyURL');
        arr = new Array();
        arr.push(url);
        arr = JSON.stringify(arr);
        localStorage.historyURL = arr;
      }else{
        arr = localStorage.historyURL;
        if(arr == undefined || arr == null || arr == ''){
          arr = new Array();
        }else{
          arr = eval(arr);
        }
        //比较最后一条是否相同（待改进，可以通过控制器方法名判断）
        lasturl = arr.pop();
        if(url == lasturl){
          arr.push(lasturl);
        }else{
          arr.push(lasturl);
          arr.push(url);
        }
        arr = JSON.stringify(arr);
        localStorage.historyURL = arr;
      }
    }
 

  $(document).on("click",".back",function(){
      back();
  });

  main.prototype.back = function(){
    back();
  }

  var back = function(){

    // if(isWeb) {
      arr = localStorage.historyURL;
      arr = eval(arr);
      backurl = arr.pop();
      arr = JSON.stringify(arr);
      localStorage.historyURL = arr;
      if(backurl == '' || backurl == null || backurl == undefined){
        backurl = baseUrl + '/site/index';
      }
      window.location.href = backurl;
    // } else {
    //    setTimeout(function(){Jockey.send("DidBack-" + urlString)},appDelay2);
    // }
  }

  main.prototype.SwitchTime = function(param){
      var timestamp = parseInt(param.substr(6,13));
      var d = new Date(timestamp); 
      return d.getFullYear() + '-' + d.getMonth()+1;
  }

    //返回URL控制器名
    main.prototype.UrlC=function(content){
      u = content.split("?");
      url_main = u[0].split("/");
      url_main.pop();
      controller = url_main.pop();
      return controller;
    }
    //返回URL方法名
    main.prototype.UrlM=function(content){
      u = content.split("?");
      url_main = u[0].split("/");
      method = url_main.pop();
      return method;
    }

    main.prototype.forTime = function(timestamp){
      var timestamp = parseInt(timestamp.substr(6, 13));
      var data = new Date(timestamp);
      
      var year=data.getFullYear();
      var month=data.getMonth()+1;
      var day=data.getDay()+1;
      var h=data.getHours();
      h=h<10?('0'+h):h;
      var i=data.getMinutes();
      i=i<10?('0'+i):i;
      var s=data.getSeconds();
      s=s<10?('0'+s):s;
      return h+':'+i+':'+s;
    }   
    
   
   


});
  
   //将获取到的  /Date(1451114192000+0800)/ 格式的时间转化成时间戳
    var DoTime = function(Dtime){
      var NewTime = new Date();
      if(Dtime){
          if(Dtime.indexOf('D') < 0){
            return formatDate(NewTime,new Date(Dtime));
          }else{
            var PublishTime = parseInt(Dtime.substr(6, 13));
            var PublishTime = new Date(PublishTime);
            return formatDate(NewTime,PublishTime);
          }
      } 
    }
      //通用时间处理
     function formatDate(NewTime,PublishTime) { 
            var result = '';
            var Pdate  = PublishTime.getDate();             //发布时的日
            var Ndate  = NewTime.getDate();                 //现在日
            var Ddata  = Ndate-Pdate;                      //相差天数
            var Pmonth = PublishTime.getMonth()+1;         //发布的月份
            var Nhours=NewTime.getHours();                 //现在的时
            var Phours=PublishTime.getHours();             //发布的时
            var Dhours  = Nhours-Phours;                   //相差天数
            var Nminutes=NewTime.getMinutes();
            var Pminutes=PublishTime.getMinutes();
            var Dminutes=Nminutes - Pminutes;
            var time_distance = NewTime-PublishTime;
            var minute = 1000 * 60;
            var hour = minute * 60;
            var day = hour * 24;
            var halfamonth = day * 15;
            var month = day * 30;
            var monthC = time_distance/month;
            var weekC = time_distance/(7*day);
            var dayC = time_distance/day;
            var hourC = time_distance/hour;
            var minC = time_distance/minute;
            var sec  = time_distance/1000;
            if(monthC>=1){
                result = Pmonth+"-"+Pdate;
            }else{
                if(Ddata>2){   
                    if(Pdate<10){
                        Pdate = "0"+Pdate;
                    }                     
                    result = Pmonth+"-"+Pdate;            //当返回的天数差值大于2天时  返回 月：日

                }else if(Ddata==2){                  
                    result = '前天';                      //当返回的天数差值等于2天时  返回 前天  

                }else if(Ddata==1){
                    result = '昨天';                      //当返回的天数差值等于1天时  返回 昨天 

                }else if(Dhours>=1){
                    if(Phours>=12){
                        Phours = Phours-12;
                        if(Pminutes<10){

                             Pminutes = "0"+Pminutes;
                        }
                        result = "下午 "+Phours+":"+Pminutes;                 //当返回天数的差值大于或等于1小时 返回  时：分
                    }else{
                        if(Pminutes<10){
                            Pminutes = "0"+Pminutes;
                        }
                        result = "上午 "+Phours+":"+Pminutes;                   //当返回天数的差值大于或等于1小时 返回  时：分
                    }    
                }else  if(minC>=3){
                   if(Phours>=12){
                        Phours = Phours-12;
                        if(Pminutes<10){
                             Pminutes = "0"+Pminutes;
                             
                        }
                        result = "下午 "+Phours+":"+Pminutes;                 //当返回时间的差值大于或等于1小时 返回  时：分
                    }else{
                        if(Pminutes<10){
                            Pminutes = "0"+Pminutes;

                        }
                        result = "上午 "+Phours+":"+Pminutes;                   //当返回时间的差值大于或等于1小时 返回  时：分
                    }    
                }else{
                    result = '刚刚';
                }
            }    
            return result;
    }
// 手机反馈
$(document).on('click','.reback',function(){
  var _this = $(this);
    _this.addClass("bf6");
   setTimeout(function(){
    _this.removeClass("bf6");
   },100)
});

$('.reback').click(function(){
  var _this = $(this);
    _this.addClass("bf6");
   setTimeout(function(){
    _this.removeClass("bf6");
   },100)
});
//Yuri 点击更换背景
$(document).on("touchstart",".handhover",function(){
    $(this).addClass("bg-handhover");
});
$(document).on("touchend",".handhover",function(){
    var _obj = $(this);
    setTimeout(function(){
        _obj.removeClass("bg-handhover");
    },200);
});



 //通用点击事件
  $('.skips').click(function(){ 
      var linkUrl= $(this).attr('data-src');
      UrltoStorage();
      window.location.href= linkUrl;
  });

  $(document).on("click",".skip",function(){
      var linkUrl=$(this).attr('value');
      window.location.href= linkUrl;
  });

  $(document).on("click",".skipn",function(){
      var linkUrl=$(this).attr('data-src');
      window.location.href= linkUrl;
  });

  $(document).on("click",".skips",function(){ 
      var linkUrl= $(this).attr('data-src');
      UrltoStorage();
      window.location.href= linkUrl;
  });

  $(document).on("click",".skipspro",function(event){ 
        event.stopPropagation(); 
        var linkUrl= $(this).attr('data-src');
        UrltoStorage();
        window.location.href= linkUrl;
  });
 function  UrltoStorage() {
      var url = window.document.location.href.toString();
      var u = url.split("?");
      var url_main = u[0].split("/");
      var method = url_main.pop();
      var controller = url_main.pop();
      if((controller == 'site' && method == 'index')||(controller == 'site' && method == 'find')||(controller == 'personal' && method == 'index')){
        //如果当前页面为首页则清除localstorage.historyURL
        localStorage.removeItem('historyURL');
        arr = new Array();
        arr.push(url);
        arr = JSON.stringify(arr);
        localStorage.historyURL = arr;
      }else{
        arr = localStorage.historyURL;
        if(arr == undefined || arr == null || arr == ''){
          arr = new Array();
        }else{
          arr = eval(arr);
        }
        //比较最后一条是否相同（待改进，可以通过控制器方法名判断）
        lasturl = arr.pop();
        if(url == lasturl){
          arr.push(lasturl);
        }else{
          arr.push(lasturl);
          arr.push(url);
        }
        arr = JSON.stringify(arr);
        localStorage.historyURL = arr;
      }
    }